package com.vpen.iot.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;

import lombok.extern.slf4j.Slf4j;

import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * @author Vpen
 * @date 2024/9/21 17:10
 */
@Slf4j
@Component
public class TrainFileUtil {

    public static final String TEMP_PATH = File.separator + "tmp" + File.separator + "release";


    public static void del(File... files) {
        for (File f : files) {
            if (Objects.nonNull(f)) {
                FileUtil.del(f);
            }
        }
    }


    public static File genFile(List<String> lines, String filePath) {
        File file = FileUtil.newFile(filePath);
        String content = String.join(System.lineSeparator(), lines);
        FileUtil.writeUtf8String(content, file);
        return file;
    }

    public static File genFile(String content, String filePath) {
        File file = FileUtil.newFile(filePath);
        FileUtil.writeUtf8String(content, file);
        return file;
    }

    public static String genPath(String prefix, String outDirName) {
        String path = prefix + File.separator + outDirName;
        String os = System.getProperty("os.name");
        if (StrUtil.containsAnyIgnoreCase(os, "windows")) {
            path = "D:" + path;
        }
        log.debug("path:{}", path);
        return path;
    }

    public static String genFilePath(String prefix, String fileName) {
        String path = prefix + File.separator + fileName;
        log.debug("file:{}", path);
        return path;
    }

    public static  File downloadImage(String url, String path) {
        try {
            File file = FileUtil.file(path);
            HttpUtil.downloadFileFromUrl(url, file);
            return file;
        } catch (Exception e) {
            log.warn("下载图片失败:{}", e.getMessage());
            return null;
        }
    }
}
